﻿using System.Collections.ObjectModel;
using System.Data;
using System.Data.SqlClient;
using Libraries.Common;
using Libraries.Entities;

namespace Libraries.Repositories
{
    public class CourseInformationEmployeeRepository
    {
        #region Information Course Details
        private CourseInfomation parseToEntityCourseInfomation(IDataRecord dataReader)
        {
            if (dataReader != null)
            {
                return new CourseInfomation()
                {
                    CourseId = dataReader["CourseId"].ToInt32OrDefault(),
                    AccountName = dataReader["AccountName"].ToStringOrDefault(),
                    Department = dataReader["DepartmentName"].ToStringOrDefault(),
                    EmployeeId = dataReader["EmployeeId"].ToInt32OrDefault(),
                    FullName = dataReader["FullName"].ToStringOrDefault(),
                    JobTitle = dataReader["JobTitleName"].ToStringOrDefault(),
                    CourseName = dataReader["CourseName"].ToStringOrDefault(),
                    Location = dataReader["LocationName"].ToStringOrDefault(),
                    TimeCommit = dataReader["TimeCommit"].ToInt32OrDefault(),
                    TrainedDuration = (decimal?)dataReader["TrainedDuration"],
                    Costs = (decimal?)dataReader["Costs"],
                    StartDate = dataReader["StartDate"].ToDateTimeOrDefault(),
                    EndDate = dataReader["EndDate"].ToDateTimeOrDefault(),
                };
            }
            return null;
        }

        public Collection<CourseInfomation> AllEmployeeByCourseId(int courseId)
        {
            var parameters = new Collection<SqlParameter>
                                 {
                                    new SqlParameter("@CourseId",courseId)
                                 };

            Collection<CourseInfomation> list = null;
            if (!string.IsNullOrEmpty(ProcedureConfiguration.SearchEmployeeByCategoryId))
            {
                using (var result = RepositorySql.SqlHelper.ExecuteReader(ProcedureConfiguration.SearchEmployeeByCategoryId, parameters, ConnectionConfiguration.ConnectionString))
                {
                    if (result != null)
                    {
                        list = new Collection<CourseInfomation>();
                        while (result.Read())
                        {
                            list.Add(parseToEntityCourseInfomation(result));
                        }
                    }
                }
            }
            return list;
        }
        #endregion 
    }
}